package com.lw.yygh.hosp.controller.api;

import com.lw.yygh.exception.YyghException;
import com.lw.yygh.hosp.common.GetParameterAndCheck;
import com.lw.yygh.hosp.repository.ScheduleRepository;
import com.lw.yygh.hosp.service.DepartmentService;
import com.lw.yygh.hosp.service.HospService;
import com.lw.yygh.hosp.service.HospSetService;
import com.lw.yygh.hosp.service.ScheduleService;
import com.lw.yygh.model.hosp.Department;
import com.lw.yygh.model.hosp.Hospital;
import com.lw.yygh.model.hosp.Schedule;
import com.lw.yygh.result.Result;
import com.lw.yygh.result.ResultCodeEnum;
import com.lw.yygh.utils.HttpRequestHelper;
import com.lw.yygh.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

//后台用户系统api
@RestController
@RequestMapping("/api/hosp")
public class HospController {
    @Autowired
    private HospService hospService;
    @Autowired
    private HospSetService hospSetService;

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private GetParameterAndCheck getParameterAndCheck;
    //上传医院接口
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, Object> map = getParameterAndCheck.getParameterAndCheck( request );
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)map.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            map.put("logoData", logoData);
        }

        //调用service方法保存到数据库
        hospService.save(map);
        return Result.ok();
    }
    //根据医院编号查询医院
    @PostMapping("/hospital/show")
    public Result getHospital (HttpServletRequest request){
        //获取传递过来的参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap( parameterMap );
        //12.获取医院系统传过来的MD5加密签名
        String sign = (String) map.get( "sign" );
        String hoscode = (String) map.get( "hoscode" );
        String sinKey=hospSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt( sinKey );
        if (!sign.equals( signKeyMD5 )){
            throw new YyghException( ResultCodeEnum.SIGN_ERROR );
        }
        Hospital hospital=hospService.getByHoscode(hoscode);
        return Result.ok( hospital );
    }
    //上传科室接口
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> map = getParameterAndCheck.getParameterAndCheck( request );//获取参数和校验
        departmentService.save(map);
        return Result.ok();
    }
    //分页查询当前医院全部科室
    @PostMapping("/department/list")
    public Result findAllDepartment(HttpServletRequest request){
        Map<String, Object> map = getParameterAndCheck.getParameterAndCheck( request );//获取参数和校验
        Page<Department> list=  departmentService.findAll(map);
        return Result.ok( list );
    }
    //根据depcode删除科室
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, Object> map = getParameterAndCheck.getParameterAndCheck( request );//获取参数和校验
        departmentService.remove(map);
        return Result.ok();
    }
    @Autowired
    private ScheduleService scheduleService;
    //上传排班
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> map = getParameterAndCheck.getParameterAndCheck( request );//获取参数和校验
        scheduleService.save(map);
        return Result.ok();
    }
    //查询排班
    @PostMapping("/schedule/list")
    public Result findAllSchedule(HttpServletRequest request){
        Map<String, Object> map = getParameterAndCheck.getParameterAndCheck( request );//获取参数和校验
        Page<Schedule> list=  scheduleService.findAll(map);
        return Result.ok( list );
    }
    //删除排班
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> map = getParameterAndCheck.getParameterAndCheck( request );//获取参数和校验
        scheduleService.remove(map);
        return Result.ok();
    }
}
